//index.js
//获取应用实例
var app = getApp()
Page({ 
  data: {
    motto: 'Hello World',
    userInfo: {},
    serviceItem: [],
     show_nav: "h_nav",
     selectPerson: true,
     firstPerson: '',
     selectArea: false,
     levels:[],
     level_id:'',
     head_icp:'',
    
     selecter:'选择店员等级',
     

  },
  //点击选择类型
  clickPerson: function () {
    var selectPerson = this.data.selectPerson;
    if (selectPerson == true) {
      this.setData({
        selectArea: true,
        selectPerson: false,
      })
    } else {
      this.setData({
        selectArea: false,
        selectPerson: true,
      })
    }
  },
  //点击切换
  clickPerson: function (e) {
    
    var that = this;
    that.setData({
      level_id: that.data.levels[e.detail.value].level_id,
      selecter: that.data.levels[e.detail.value].name
    });
   
    
  },
   show_nav: function (e) {
    
          var that = this;
          that.setData({
            show_nav: 'Haircut_nav'
          });
  },
  onLoad: function (e) { 
    var that = this;
    //调用应用实例的方法获取全局数据
   
    
    that.setData({
      userInfo: wx.getStorageSync("userInfo"),
    })
    var pages = getCurrentPages();
                  var prevPage = pages[pages.length - 2]; 
                  prevPage.setData({
                    show_nav: 'h_nav'
                  });

      wx.showLoading({
        title: '加载中',
      }),
      wx.request({
      url: app.globalData.api_url + "index.php?ac=hapi&ts=haird_list",
        data: {hid:1},
        header: { 'content-type': 'application/json' },
        success: function (res) {
          wx.hideLoading();
          that.setData({ 
            hairdList: res.data
          })
          console.log(res.data);
        }

      });
      wx.request({
        url: app.globalData.api_url + "index.php?ac=hapi&ts=leves_get_all",
        data:{},
        method: "GET",
        header: { 'content-type': 'application/json' },
        success:function(rs){
          console.log(rs);
          that.setData({
            levels: rs.data
          })
        }
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 3000)
  },

  hairdAdd: function (e) { 
    var that = this;
    if (!this.data.head_icp){
      wx.showToast({
        title: '操作失败!',
        icon: 'loading',
        duration: 2000
      });
      return;
    }
    var formData ={
      name: e.detail.value.haird_name,
        level_id:that.data.level_id,
          phone_num: e.detail.value.phone_num,
            haird_num: e.detail.value.haird_num,
        
      };
    console.log(this.data.head_icp);
    this.upload_file(app.globalData.api_url + "index.php?ac=hapi&ts=haird_add", this.data.head_icp, 'file', formData,function(res){

      console.log(res);
      var m = JSON.parse(res);
     
      if (m.result == 0) {
        wx.showToast({
          title: '操作失败!',
          icon: 'loading',
          duration: 2000
        });
        return;
      } else {
        wx.showToast({
          title: '操作成功!',
          icon: 'success',
          duration: 2000
        });
        that.setData({
          hairdList: m.list
        });
      };
     
      setTimeout(function () {
        wx.hideToast()
      }, 5000); 


    })
    },
  hairdDel:function( e ){
    console.log(e.currentTarget.dataset);
    var that = this;
    wx.request({
      url: app.globalData.api_url + "index.php?ac=hapi&ts=haird_del",
      data: {
        hid: e.currentTarget.dataset.hairdId
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        if (res.data.result == 0) {
          wx.showToast({
            title: '操作失败!',
            icon: 'loading',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '操作成功!',
            icon: 'success',
            duration: 2000
          })
        };
        that.setData({
          hairdList: res.data.list
        });
        setTimeout(function () {
          wx.hideToast()
        }, 2000);
      }

    })
  },
  toAddPersonalItem:function( e ){
      wx.navigateTo({ 
        url: "/pages/m_personal_item/index?hid=" + e.currentTarget.dataset.hairdId
      });
  },
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: [type],
      success: function (res) {
        wx.showToast({
          title:'图片上传成功',
          icon:'success',
          duration: 2000
        });
        that.setData({
          head_icp: res.tempFilePaths[0]
        });
      }
    });
  },
  chooseImageTap: function () {
    var that = this;
    wx.showActionSheet({

      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {

        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }

      }

    });
  },
  upload_file: function (url, filePath, name, formData, success, fail) {

    wx.uploadFile({
      url: app.globalData.api_url + "index.php?ac=hapi&ts=haird_add",
      filePath: filePath,
      name: name,
      header: {
        'content-type': 'nultipart/form-data'
      },
      formData: formData,  //http请求中其他额外的form data
      success: function (res) {

        if (res.statusCode == 200 && !res.data.result_code) {
          typeof success == "function" && success(res.data);
        } else {
          typeof fail == "function" && fail(res);
        }
      },
      fail: function (res) {

        typeof fail == "function" && fail(res);
      }
    })
  }

})
